﻿namespace LogManager.Gui.ViewModels
{
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Input;
    using LogManager.Core.Infrastructure.Logging;
    using LogManager.Core.Models;
    using LogManager.Gui.Factories;
    using LogManager.Gui.Helpers;
    using LogManager.Gui.Views;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ServerViewModel : ViewModelBase<IServerView>, IServerViewModel
    {
        public IServer Server { get; private set; }
        public ReadOnlyObservableCollection<ILocationViewModel> LocationsVm { get; private set; }
        public readonly IMainViewModel MainViewModel;
        public ServerViewModel(IServerView view, IMainViewModel mainViewModel, IServer server, IViewModelFactory viewModelFactory)
            : base(view)
        {
            this.MainViewModel = mainViewModel;
            this.Server = server;
            this.LocationsVm = Server.Locations.GetSyncWrapper(m => viewModelFactory.CreateLocationViewModel(m));
        }

        #region Commands

        public ICommand DeleteServerCommand { get { return new DelegateCommand(OnDeleteServer); } }
        private void OnDeleteServer()
        {
            this.Server.DeleteServer();
            Log.Logger.AddInfo("Serveur supprimé", this.Server.Name);
        }

        public ICommand AddLocationCommand { get { return new DelegateCommand(OnAddLocation); } }
        private void OnAddLocation()
        {
            var newLocation = this.Server.AddNewLocation();
            this.MainViewModel.View.ExpandAndSelectItem(LocationsVm.Single(p => p.Location == newLocation));
            Log.Logger.AddInfo("Répertoire créé", newLocation.Name);
        }

        #endregion
    }
}
